
// 103.二叉树的锯齿形层序遍历
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        // 层序遍历 , 只不过每个一层数组反转一次
        vector<vector<int>> ret;
        queue<TreeNode*> q;
        if(root) q.push(root);
        int count = 0;
        while(q.size())
        {
            int sz = q.size();
            vector<int> tmp;
            for(int i = 0 ; i < sz ; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                tmp.push_back(top->val);

                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
            if(count%2 == 1) reverse(tmp.begin() , tmp.end());
            ret.push_back(tmp);
            count++;
        }
        return ret;
    }
};